/*=========================== SOFTWARE LICENSE AGREEMENT ===========================
 
    Copyright (c) 2012, Rob Lammert Software
 
    This file is part of SharpLog.
    Author:  Rob Lammert <rob@roblammert.com>

    Foobar is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
  =================================================================================*/
using System;
using System.Collections.Generic;
using System.Text;

namespace com.roblammert.sharplog.Outputs
{
	/// <summary>
	/// MemoryLogger is a Logger Observer class which stores the log data in a memory buffer (String List).
	/// </summary>
	public class MemoryLogger : ILogger
	{
        #region Data

		//private StringBuilder mLogBuffer;
		private List<String> mLogBuffer;
		private static object mLock = new object();
		private static MemoryLogger mLogger = null;
		
		/// <summary>
		/// Gets the instance of MemoryLogger.
		/// </summary>
		/// <value>
		/// The instance.
		/// </value>
		public static MemoryLogger Instance
		{
			get
			{
				// If this is the first time we’re referring to the
				// singleton object, the private variable will be null.
				if ( mLogger == null )
				{
					// for thread safety, lock an object when
					// instantiating the new Logger object. This prevents
					// other threads from performing the same block at the
					// same time.
					lock ( mLock )
					{
						// Two or more threads might have found a null
						// mLogger and are therefore trying to create a 
						// new one. One thread will get to lock first, and
						// the other one will wait until mLock is released.
						// Once the second thread can get through, mLogger
						// will have already been instantiated by the first
						// thread so test the variable again. 
						if ( mLogger == null )
						{
							mLogger = new MemoryLogger();
						}
					}
				}
				return mLogger;
			}
		}
		
		/// <summary>
		/// Gets the log buffer contents.
		/// </summary>
		/// <value>
		/// The log buffer contents.
		/// </value>
		public List<String> LogBufferContents
		{
			get
			{
				//return mLogBuffer.ToString(); 
				return mLogBuffer;
			}
		}
        #endregion

        #region Constructor
		private MemoryLogger()
		{
			//mLogBuffer = new StringBuilder();
			mLogBuffer = new List<String>();
		}
        #endregion

        #region ILogger Members

        /// <summary>
        /// Write Log event to a buffer in memory
        /// </summary>
        /// <param name="eventTime">Date and time that the event took place</param>
        /// <param name="level">Severity Level of the event</param>
        /// <param name="facility">Facility of the event</param>
        /// <param name="method">Method that called the Logger class instance</param>
        /// <param name="logMessage">Message detailing the event</param>
        public void ProcessLogMessage(DateTime date, Logger.Severity level, Logger.Facility facility, String method, String message)
		{
			mLogBuffer.Add (date.ToString() + " - [" + level.ToString() + ":" + facility.ToString() + "] <" + method + "> " + message);
		}
        #endregion
	}
}

